package modele;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;

@Entity
public class Sport {

	@Id
	@Field(index=Index.UN_TOKENIZED)
	@GeneratedValue(strategy=GenerationType.AUTO)
	private long id;
	
	private String name;

	@OneToMany(mappedBy="sport",fetch=FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	private Collection<Competition> competitions;
	
	@OneToMany(mappedBy="sport",fetch=FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	private Collection<Article> articles;
	
	public Sport(){
		this.setArticles(new ArrayList<Article>());
		this.competitions= new ArrayList<Competition>();
	}
	

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setCompetitions(Collection<Competition> competitions) {
		this.competitions = competitions;
	}

	public Collection<Competition> getCompetitions() {
		return competitions;
	}


	public ArrayList<Article> getArticlesTriesDate() {
		ArrayList<Article> liste=new ArrayList<Article>();
		Collection<Competition> cpts = this.competitions;
		for(Competition cpt : cpts){
			liste.addAll(cpt.getArticles());
		}
		Collections.sort(liste,new ComparatorArticleDate());
		return liste;
	}

	public ArrayList<Article> getArticlesTriesNote() {
		ArrayList<Article> liste=new ArrayList<Article>();
		Collection<Competition> cpts = this.competitions;
		for(Competition cpt : cpts){
			liste.addAll(cpt.getArticles());
		}
		Collections.sort(liste,new ComparatorArticleNote());
		return liste;
	}


	public void setArticles(Collection<Article> articles) {
		this.articles = articles;
	}


	public Collection<Article> getArticles() {
		return articles;
	}
	
	
	
	
}
